<?php
/*
 * @分销商城
 * @功能说明：API模块通用函数
 * @更新说明：暂无更新
 * @文件名 function.php
 * @编码 UTF-8
 * @创建时间 2015-6-8 下午3:27:46
 * @创建人 XING
 */

/*
 * API接口数据格式化输出
 * @date: 2015-6-8 下午3:29:27
 * @editor: XING
 */
if(!function_exists('dataFormat')){
	function dataFormat($data=array(),$recode=1,$msg='',$adds=array()){
		if($data || $adds){
			echo json_encode(array_merge(array(
					'data' => $data,
					'code' => $recode,
					'msg' => $msg ? $msg : 'success'
				),$adds));exit;
		}else{
			echo json_encode(array_merge(array(
					'code' => $recode,
					'msg' => $msg ? $msg : '没有相关数据'	
				)));exit;
		}
	}
}

/*
 * 返回随机字符串
 * @date: 2015-3-21下午3:34:47
 * @editor: XING
 */
if(!function_exists('randstr')){
    function randstr($num=1){
        $return = '';
        $randstr = '1023456789abcdefghijklmnopqrstuvwxyz';
        for($i=0;$i<$num;$i++){
            $return .= $randstr{rand(0,35)};
        }

        return $return;
    }
}

/*
 * 订单生成码
 * @date: 2015-6-4 下午4:51:01
 * @editor: XING
 */
if(!function_exists('mkorder')){
	function mkorder(){
		$creatOid = date("Y").substr(time(),4).rand(10000,99999);
		$isExists = M('OrderEntity')->where(array('order_id'=>$creatOid))->count();
		if($isExists > 0)
			self::mkorder();
		else
			return $creatOid;
	}
}

/*
 * 敏感词汇过滤
 * @date: 2015-9-9 上午10:59:47
 * @editor: XING
 */
if(!function_exists('wordFilter')){
	function wordFilter($words){
		$configSet = getSysConfig('senSitive');
		if($words && $configSet){			
			foreach(explode(',',$configSet) AS $val){
				if(strstr($words,$val)){
					dataFormat('',0,'含敏感词汇');
				}
			}
		}
	}
}

/*
 * 密钥生成
 * @date: 2016-3-10 下午3:50:24
 * @editor: XING
 */
if(!function_exists('mySpreadSek')){
	function mySpreadSek($cust_id){
		$sekSecret = substr(md5($cust_id),5,6).$cust_id;
		return $sekSecret;
	}
}

/*
 * 密钥解密
 * @date: 2016-3-10 下午3:50:24
 * @editor: XING
 */
if(!function_exists('spotSek')){
	function spotSek($keys){
		if($keys && $keys == substr(md5(substr($keys,6)),5,6).substr($keys,6)){
			return substr($keys,6);
		}else{
			return false;
		}
	}
}

/*
 * 检测参数合法性
 * @date: 2016-3-23 下午12:02:28
 * @editor: XING
 */
if(!function_exists('forLegal')){
	function forLegal($sek){
		if(!$sek){
			redirect('/saler/noauth?msg=访问出错，请返回~');
		}
	}
}

/*
 * 检测登陆
 * @date: 2016-4-4 下午3:21:44
 * @editor: XING
 */
if(!function_exists('is_login')){
	function is_login(){

        global $isLogin;

//        if(!session('loginMobile') || !session('loginName') || !session('loginUid')) {
            $isLogin = false;
//            }
        $loginCookie = cookie('ajwUid');
        if(isset($loginCookie) && $loginCookie) {
            $loginUid = base64_decode($loginCookie);
            if (session($loginUid)) {
                $isLogin = true;
            } else {
                $loginUid = base64_decode(cookie('ajwUid'));
                $rememberNum = M('UserInfo')->where(array('user_mobile' => $loginUid))->find();
                if ($rememberNum['remember_token'] == cookie('_ajwunion')) {
                    $isLogin = true;
                }
            }
        }
        return $isLogin;
	}
}
?>